﻿using System;
using System.Linq;
using Core.Contract.DataContract;
using www.Models;

namespace Data.DAO
{
    public class LinkValueDAO//:BaseDAO<RELAXEntities,RelaxLink>
   {
       #region value
       private readonly RelaxDataContext _context = new RelaxDataContext();
        LinkRefDAO _linkRefDao=new LinkRefDAO();
       #endregion

       #region Hàm lấy dữ liệu
      
       #endregion
       #region Add
       public void Add(LINK_VALUE link)
       {
           _context.LINK_VALUEs.InsertOnSubmit(link);
           _context.SubmitChanges();

       }
       #endregion
       #region Update

       public bool Update(LINK_VALUE link)
       {
           var first = _context.LINK_VALUEs.FirstOrDefault(p => p.linkid == link.linkid && p.languagecode==link.languagecode);
           if (first != null)
           {
               first.name = link.name;
               first.keyword = link.keyword;
               first.decription = link.decription;
               _context.SubmitChanges();
               return true;
           }
           return false;
       }
       #endregion

       #region Delete

       public void Delete(long iId, long iLanguageCode)
       {
           var lData = _context.LINK_VALUEs.Where(p => p.linkid == iId);
           var deleteRef = lData.Count() == 1 || iLanguageCode == 0;

           if (iLanguageCode != 0)
               lData = lData.Where(p => p.languagecode == iLanguageCode);

           foreach (var item in lData)
           {
               _context.LINK_VALUEs.DeleteOnSubmit(item);
           }
           _context.SubmitChanges();
           if (deleteRef)
                _linkRefDao.Delete(iId);
       }
       #endregion
   }
}
